package final_wearebatman_view;

import final_wearebatman_model.Switch;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

public class SwitchView implements Ipaintable {

    private Switch sw;
    private CircuitView circuitview;
    private int posX;
    private int posY;
    private int angle;
    private String name;

    // Konstruktor melben összekötjük a view-ot a modellel
    public SwitchView(Switch sw, String name, CircuitView cv, int pX, int pY, int a) {
        this.sw = sw;
        this.circuitview = cv;
        this.posX = pX;
        this.posY = pY;
        this.angle = a;
        this.name = name;
    }

    // kirajzolás folyamata
    public void paint(Graphics2D g) {
        AffineTransform aT = g.getTransform();
        g.translate(posX, posY);
        g.rotate(this.angle*Math.PI/180);
        g.drawRect(0, 0, 20, 16);
        
        if(this.sw.getOutputData(0)==1)
            g.setColor(Color.red);
        else
            g.setColor(Color.white);
        g.fillRect(2, 2, 16, 12);
        g.setColor(Color.black);
        g.drawRect(2, 2, 16, 12);
        g.drawLine(20, 8, 28, 8);
        g.setTransform(aT);
    }

    public int getPosX() {
        return this.posX;
    }

    public int getPosY() {
        return this.posY;
    }

    public int getAngle() {
        return this.angle;
    }

    public Point getInputPinPosition(int id) {
        return new Point(posX,posY);
    }

    public Point getOutputPinPosition(int id) {
        Point p = new Point(28, 8);
        AffineTransform aT = new AffineTransform();
        aT.rotate(this.angle * Math.PI / 180);
        aT.transform(p, p);
        return new Point(posX + p.x, posY + p.y);
    }

    public Rectangle getClickArea() {
        return new Rectangle(posX,posY,20,16);
    }

    public void onClick() {
        sw.toggleState();
    }

}
